/**
 * 
 */
package com.es.disped.web.controller.reset;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.es.disped.api.sms.SmsService;
import com.es.disped.api.user.UserService;
import com.es.disped.common.constant.AliSms;
import com.es.disped.common.model.MsgModel;
import com.es.disped.core.annotation.LogInject;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.User;
import com.es.disped.dao.model.UserExample;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author Anson<br>
 *
 * Copyright by EasyShare 2019<br>
 *
 * All right reserved<br>
 *
 * Created on 下午8:43:07<br>
 *
 * 名称：ResetController.java<br>
 *
 * 描述：用户密码重置<br>
 */
@Api("用户密码重置")
@Controller
@RequestMapping("/reset")
public class ResetController extends BaseController {

	@LogInject
	Logger log;
	@Autowired
	SmsService smsService;
	@Autowired
	UserService userService;
	@Autowired
	RedisTemplate<String, String> redisTemplate;
	
	
	
	@ApiOperation(value="重置密码人员手机号确认",httpMethod="POST",response=MsgModel.class,
			notes="用户重置密码，先通过向手机号发送验证码的形式，确认用户账号是否存在，且手机号是否确证")
	@ApiImplicitParam(name="phone",value="手机号",required=true,dataType="String")
	@RequestMapping(value="/vertify",method=RequestMethod.POST)
	public @ResponseBody MsgModel resetUserVertify(@RequestBody Map<String, Object> paramMap)
	{
    	UserExample userExample=new UserExample();
    	userExample.createCriteria().andPhoneEqualTo(paramMap.get("phone").toString());
    	List<User> users=userService.selectByExample(userExample);
    	return new MsgModel((!this.isNull(users))?HttpStatus.OK.value():HttpStatus.NOT_ACCEPTABLE.value(),
    			(!this.isNull(users))?"":"手机号不存在");
	}
	
	
	
	@ApiOperation(value="密码重置",httpMethod="POST",response=MsgModel.class,
			notes="重置密码，提交用户手机验证码，用户手机号，以及新密码")
	@ApiImplicitParams({
		@ApiImplicitParam(name="code",value="验证码",required=true,dataType="String"),
		@ApiImplicitParam(name="phone",value="手机号",required=true,dataType="String"),
		@ApiImplicitParam(name="password",value="密码",required=true,dataType="String")
	})
	@RequestMapping(value="/password",method=RequestMethod.POST)
	public @ResponseBody MsgModel reset(@RequestBody Map<String, Object> paramMap)
	{
		int count=0;
		User user=new User();
		if(Optional.of(paramMap)
				.filter(code->paramMap.get("code")!=null)
				.filter(phone->paramMap.get("phone")!=null)
				.filter(password->paramMap.get("password")!=null)
				.isPresent()
				&&redisTemplate.hasKey(paramMap.get("phone").toString()))
		{
			user.setPassword(this.md5(paramMap.get("password").toString()));
			UserExample userExample=new UserExample();
			userExample.createCriteria().andPhoneEqualTo(user.getPhone());
			count=userService.updateByExampleSelective(user, userExample);
			redisTemplate.delete(paramMap.get("phone").toString());
		}
		//重置成功，短信通知用户
		if(count==1)
		{
			JSONObject paramJson=new JSONObject();
			paramJson.put("password", paramMap.get("password"));
			smsService.sendMessage(
					AliSms.ALI_MESSAGE_SIGN_NAME.ES_TECH.getValue(), 
					paramMap.get("phone").toString(),
					AliSms.ALI_MESSAGE_TEMPLATE_CODE.PASSWORD_RESET.getValue(),
					paramJson.toJSONString());
		}
		return new MsgModel(count==1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count==1?"":"密码重置失败");
	}
	
	
	
	@ApiOperation(value="新密码短信通知",httpMethod="POST",response=MsgModel.class,
			notes="用户重置密码，将密码通过短信形式发送至手机")
	@RequestMapping(value="/notice")
	public @ResponseBody MsgModel resetNotice(@RequestBody Map<String, Object> paramMap)
	{
		
		return new MsgModel();
	}
}
